﻿Imports System.Windows.Forms

Public Class GroupEditDialog
    Private Sub GroupEditDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillFormGridView()
        FillProviderTypesGridView()
        FillUsersGridView()
    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub FillFormGridView()
        Dim SQL As String
        Dim TheView As DataView

        SQL = "select A.Form_ID, A.Form_Name as [Form Name], " & _
                "case when A.Access_Level = 'N' then 1 else 0 end as [No Access], " & _
                "case when A.Access_Level = 'R' then 1 else 0 end as [Read], " & _
                "case when A.Access_Level = 'W' then 1 else 0 end as [Write] " & _
                "from  " & _
                "	( " & _
                "	select	GFA.Form_ID, F.Form_Name, GFA.Access_Level " & _
                "	from	dbo.Group_Form_Access GFA " & _
                "	left outer join dbo.Forms F on GFA.Form_ID = F.FORM_ID where Group_ID = '" & Group_IDTextBox.Text & "' " & _
                "	union " & _
                "	select	Form_ID, Form_Name, Default_Access_Level as Access_Level " & _
                "	from	dbo.Forms " & _
                "	where Form_ID not in (select Form_ID from dbo.Group_Form_Access where Group_ID = '" & Group_IDTextBox.Text & "') " & _
                "	) as A order by A.Form_Name"

        TheView = LoadDataView(SQL)

        FormGridView.DataSource = TheView

        'format the grid

        With FormGridView

            Dim index As Integer

            .Columns("Form_ID").Visible = False
            .Columns("Form Name").ReadOnly = True

            'No Access Column

            Dim NoAccessColumn As New DataGridViewCheckBoxColumn

            NoAccessColumn.Name = "No Access"
            NoAccessColumn.DataPropertyName = "No Access"

            NoAccessColumn.ThreeState = False
            NoAccessColumn.TrueValue = 1
            NoAccessColumn.FalseValue = 0
            NoAccessColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
            NoAccessColumn.Width = 75

            index = .Columns.IndexOf(.Columns("No Access"))
            .Columns.RemoveAt(index)
            .Columns.Insert(index, NoAccessColumn)

            'Read Column

            Dim ReadColumn As New DataGridViewCheckBoxColumn

            ReadColumn.Name = "Read"
            ReadColumn.DataPropertyName = "Read"

            ReadColumn.ThreeState = False
            ReadColumn.TrueValue = 1
            ReadColumn.FalseValue = 0
            ReadColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
            ReadColumn.Width = 75

            index = .Columns.IndexOf(.Columns("Read"))
            .Columns.RemoveAt(index)
            .Columns.Insert(index, ReadColumn)

            'Write Column

            Dim WriteColumn As New DataGridViewCheckBoxColumn

            WriteColumn.Name = "Write"
            WriteColumn.DataPropertyName = "Write"

            WriteColumn.ThreeState = False
            WriteColumn.TrueValue = 1
            WriteColumn.FalseValue = 0
            WriteColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
            WriteColumn.Width = 75

            index = .Columns.IndexOf(.Columns("Write"))
            .Columns.RemoveAt(index)
            .Columns.Insert(index, WriteColumn)

        End With

    End Sub

    Public Sub ClearForm()
        Me.Text = "New Group"

        Group_IDTextBox.Text = ""
        Group_NameTextBox.Text = ""

    End Sub

    Public Sub LoadForm()
        Me.Text = "Edit Group"


    End Sub

    Public Sub SaveForm()

        ShowLoadingForm()

        'Save Group Names
        Dim SQL As String

        If Me.Text = "Edit Group" Then
            SQL = "update Groups set Group_Name = '" & Group_NameTextBox.Text & "' where Group_ID =  '" & Group_IDTextBox.Text & "'"
        Else
            SQL = "insert into Groups values ('" & Group_IDTextBox.Text & "','" & Group_NameTextBox.Text & "')"
        End If

        RunNonQuery(SQL)

        'Save Form Access
        Dim i As Integer
        Dim AccessLevel As String

        For i = 0 To FormGridView.Rows.Count - 1

            If FormGridView.Item(FormGridView.Columns("No Access").Index, i).Value = 1 Then
                AccessLevel = "N"
            ElseIf FormGridView.Item(FormGridView.Columns("Read").Index, i).Value = 1 Then
                AccessLevel = "R"
            Else : AccessLevel = "W"
            End If

            SQL = "delete from Group_Form_Access where Group_ID = '" & Group_IDTextBox.Text & "' " & _
                    "and Form_ID = '" & FormGridView.Item(FormGridView.Columns("Form_ID").Index, i).Value & "' " & _
                    "insert into Group_Form_Access values ('" & Group_IDTextBox.Text & "', '" & FormGridView.Item(FormGridView.Columns("Form_ID").Index, i).Value & "', '" & AccessLevel & "')"

            RunNonQuery(SQL)

        Next

        'Save Provider Types Access
        For i = 0 To ProviderTypesGridView.Rows.Count - 1

            If ProviderTypesGridView.Item(ProviderTypesGridView.Columns("Change").Index, i).Value = 1 Then
                SQL = "insert into Provider_Type_Groups values ('" & Group_IDTextBox.Text & "' " & _
                        ", '" & ProviderTypesGridView.Item(ProviderTypesGridView.Columns("Provider_Type_Code").Index, i).Value & "')"
            Else
                SQL = "delete from Provider_Type_Groups where Group_ID = '" & Group_IDTextBox.Text & "' and " & _
                        " Provider_Type_Code = '" & ProviderTypesGridView.Item(ProviderTypesGridView.Columns("Provider_Type_Code").Index, i).Value & "'"
            End If

            RunNonQuery(SQL)
        Next

        'Save User Names
        For i = 0 To UsersGridView.Rows.Count - 1

            If UsersGridView.Item(UsersGridView.Columns("Group Member").Index, i).Value = 1 Then
                SQL = "insert into Users_Groups values ('" & UsersGridView.Item(UsersGridView.Columns("User Name").Index, i).Value & "' " & _
                        ", '" & Group_IDTextBox.Text & "')"
            Else
                SQL = "delete from Users_Groups where Group_ID = '" & Group_IDTextBox.Text & "' and " & _
                        "User_Name = '" & UsersGridView.Item(UsersGridView.Columns("User Name").Index, i).Value & "'"
            End If

            RunNonQuery(SQL)
        Next

        ClearForm()
        HideLoadingForm()

    End Sub

    Private Sub FormGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles FormGridView.CellContentClick

        If FormGridView.Item(e.ColumnIndex, e.RowIndex).Value = 0 Then
            If e.ColumnIndex <> FormGridView.Columns("No Access").Index Then
                FormGridView.Item(FormGridView.Columns("No Access").Index, e.RowIndex).Value = 0
            End If

            If e.ColumnIndex <> FormGridView.Columns("Read").Index Then
                FormGridView.Item(FormGridView.Columns("Read").Index, e.RowIndex).Value = 0
            End If

            If e.ColumnIndex <> FormGridView.Columns("Write").Index Then
                FormGridView.Item(FormGridView.Columns("Write").Index, e.RowIndex).Value = 0
            End If
        End If

    End Sub

    Private Sub FormGridView_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles FormGridView.CellEndEdit
        Dim check As Integer = 0

        With FormGridView
            check = .Item(.Columns("No Access").Index, e.RowIndex).Value
            check = check + .Item(.Columns("Read").Index, e.RowIndex).Value
            check = check + .Item(.Columns("Write").Index, e.RowIndex).Value

            If check = 0 Then
                .Item(.Columns("No Access").Index, e.RowIndex).Value = 1
            End If
        End With
    End Sub

    Private Sub FillProviderTypesGridView()

        Dim SQL As String
        Dim TheView As DataView

        SQL = "select G.Group_ID, Group_Name, P.Provider_Type_Code, Provider_Type_Description as [Provider Type], Tick from ( " & _
                "select Group_ID, Provider_Type_Code, 1 as Tick from dbo.Provider_Type_Groups as PG " & _
                "where Group_ID = '" & Group_IDTextBox.Text & "' union " & _
                "select '" & Group_IDTextBox.Text & "' as Group_ID, Provider_Type_Code, 0 as Tick " & _
                "from Provider_Types " & _
                "where Provider_Type_Code not in (Select Provider_Type_Code from Provider_Type_Groups where Group_Id = '" & Group_IDTextBox.Text & "') " & _
                ") as PG " & _
                "left outer join Groups as G on PG.Group_ID = G.Group_ID " & _
                "left outer join Provider_Types as P on PG.Provider_Type_Code = P.Provider_Type_Code " & _
                "order by G.Group_ID, Provider_Type_Description"

        TheView = LoadDataView(SQL)

        ProviderTypesGridView.DataSource = TheView

        'format the grid

        With ProviderTypesGridView

            Dim index As Integer

            .Columns("Group_ID").Visible = False
            .Columns("Group_Name").Visible = False
            .Columns("Provider_Type_Code").Visible = False
            .Columns("Provider Type").ReadOnly = True

            Dim TickColumn As New DataGridViewCheckBoxColumn

            TickColumn.Name = "Change"
            TickColumn.DataPropertyName = "Tick"

            TickColumn.ThreeState = False
            TickColumn.TrueValue = 1
            TickColumn.FalseValue = 0

            index = .Columns.IndexOf(.Columns("Tick"))
            .Columns.RemoveAt(index)
            .Columns.Insert(index, TickColumn)

        End With

    End Sub

    Private Sub FillUsersGridView()

        Dim SQL As String
        Dim TheView As DataView

        SQL = "select Group_ID, User_Name as [User Name], 1 as Member from dbo.Users_Groups where Group_ID = '" & Group_IDTextBox.Text & "' " & _
                "union select '" & Group_IDTextBox.Text & "' as Group_ID, User_Name as [User Name], 0 as Member from Users " & _
                "where User_Name not in (select User_Name from dbo.Users_Groups where Group_ID = '" & Group_IDTextBox.Text & "')"

        TheView = LoadDataView(SQL)
        UsersGridView.DataSource = TheView

        With UsersGridView
            Dim index As Integer

            .Columns("Group_ID").Visible = False
            .Columns("User Name").ReadOnly = True

            Dim MembersColumn As New DataGridViewCheckBoxColumn

            MembersColumn.Name = "Group Member"
            MembersColumn.DataPropertyName = "Member"

            MembersColumn.ThreeState = False
            MembersColumn.TrueValue = 1
            MembersColumn.FalseValue = 0

            index = .Columns.IndexOf(.Columns("Member"))
            .Columns.RemoveAt(index)
            .Columns.Insert(index, MembersColumn)

        End With

    End Sub

    Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click

    End Sub
    Private Sub ProviderTypesGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles ProviderTypesGridView.CellContentClick

    End Sub
End Class
